package edu.mama.college.mapper;

import edu.mama.college.model.Resource;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository(value ="ResourceMapper")
public interface ResourceMapper {
    @Select("SELECT * FROM resource where type = #{type} order by id asc limit ${(page - 1)*size} , ${size}")
    List<Resource> getResourceList(int page,int size, String type);

    @Select("SELECT * FROM resource where type = #{type} order by id asc")
    List<Resource> getResources( String type);

    @Select("SELECT count(*) FROM resource where type = #{type}")
    int getResourceCount(String type);

    @Select("DELETE FROM resource where id = #{id}")
    void delete(int id);

    @Select("INSERT INTO resource (`type`,`name`,size,duration,url) VALUES (#{type},#{name},#{size},#{duration},#{url})")
    void create(Resource resource);

    @Select("SELECT * FROM resource where id = #{id}")
    Resource selectById(int id);

    @Select("SELECT count(uc.id) number FROM user_course uc LEFT JOIN lesson l ON l.id=uc.lesson_id LEFT JOIN resource r ON r.url=resource WHERE r.id=#{id}")
    int getNumByStudied(int id);
}
